const SERVICE = require("../../lib/js/service.js");

Page({
  data:{
    SearchNav: ["A","B","C","D","F","G","H","J","K","L","M","N","O","P","Q","R","S","T","W","X","Y","Z"],
    navItem: null,    //导航
    bandList: 6,
    showPupBoxBg: false,
    showPupBox: false
  },

  onLoad(){
    this.getCarBrandList();
  },

  /*分享*/
  onShareAppMessage(){

  },
  
  /*获取导航项*/ 
  getSearchNav(e){
    let navItem = e.currentTarget.dataset.item;
    this.setData({
      navItem
    });
  },
  
  /*获取品牌项*/
  getBrand(e){
    let td = this.data,
      brand_id = e.currentTarget.dataset.id,
      idx = e.currentTarget.dataset.item,
      cidx = e.currentTarget.dataset.index,
      brand_name = e.currentTarget.dataset.name;
      
    if (td.brandIdx == idx && td.brandCidx == cidx){
      this.setData({
        brandIdx: idx,
        brandCidx: cidx,
        showPupBoxBg: true,
        showPupBox: true
      });
    }else{
      this.getCarSeriesList({
        brand: brand_name
      }, idx, cidx, brand_name);
    }
  },

  /*获取品牌*/
  getCarBrandList(){
    wx.showNavigationBarLoading();
    let _this = this;
    SERVICE.POST(SERVICE.URL.getBrand,{},(status,res)=>{
      wx.hideNavigationBarLoading();
      console.log("获取品牌", res);
      
      if(res.data.status === "success"){
        let brandList = res.data.result;
        _this.setData({
          brandList
        });
      }else{
        wx.showToast({
          title: "获取品牌失败",
          icon: "none",
          mask: true
        });
      }
    });
  },

  /**
   * @explain getCarSeriesList 获取车系
   * @param brandId 品牌标识
   * */ 
  getCarSeriesList(params,idx,cidx,name){
    wx.showLoading({
      title: "加载中...",
      mask: true
    });
    let _this = this;
    SERVICE.POST(SERVICE.URL.getseries,params,(status,res)=>{
      wx.hideLoading();
      if (res.data.status === "success"){
        _this.setData({
          brandName: name,          
          carSeries: res.data.result,
          brandIdx: idx,
          brandCidx: cidx,
          showPupBox: true
        });
      }else{
        wx.showToast({
          title: "加载失败",
          icon: "none",
          mask: true
        });
      }
    });
  },
  
  pupCancel(){
    this.setData({
      showPupBox: false,
      showPupItem: null,
      showPupBox_2: false
    });
  },

  pupCancel2(){
    this.setData({
      showPupBox_2: false
    });
  },

  /**
   * @explain getCarSeriesList 获取车型
   * @param seriesId 车系标识
   * */
  vehicleModel(e){
    let _this = this, td = this.data, 
      series_id = e.currentTarget.dataset.id,
      index = e.currentTarget.dataset.index,
      series_name = e.currentTarget.dataset.name;

    if (td.showPupItem == index){
      _this.setData({
        showPupItem: index,
        showPupBox_2: true
      });
    }else{
      wx.showLoading({
        title: "加载中...",
        mask: true
      });
      SERVICE.POST(SERVICE.URL.getgenre, {
        brand: td.brandName,
        carseries: series_name,
      }, (status, res) => {
        console.log("获取车型",res);
        wx.hideLoading();
        if (res.data.status === "success") {
          _this.setData({
            seriesName: series_name,
            modelList: res.data.result,
            showPupItem: index,
            showPupBox_2: true
          });
        } else {
          wx.showToast({
            title: "获取失败",
            icon: "none",
            mask: true
          });
        }
      });
    }
  },

  getModle(e){
    let td = this.data;
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2]; //上一个页面
    let params = e.currentTarget.dataset.item;
    params.brandName = td.brandName;
    params.seriesName = td.seriesName;

    prevPage.onShow(params);
      wx.navigateBack();
  }
});